#include "Cube.h"


Cube::Cube(Vector3d position,int size) : RenderObject(position)
{
	color = Vector3d(255,255,255);
	maxx = position.x + size;
	minx = position.x;

	maxy = position.y + size;
	miny = position.y;

	maxz = position.z + size;
	minz = position.z;

	Vector3d min = Vector3d(minx,miny,minz);
	Vector3d max = Vector3d(maxx,maxy,maxz);
	
	data[0] = PlanePart(position + Vector3d(0,0,size),Vector3d(0,0,1),min,max,0,0,1);
	data[1] = PlanePart(position,Vector3d(0,0,-1),min,max,0,0,1);

	data[2] = PlanePart(position + Vector3d(0,size,0),Vector3d(0,1,0),min,max,0,1,0);
	data[3] = PlanePart(position,Vector3d(0,-1,0),min,max,0,1,0);

	data[4] = PlanePart(position + Vector3d(size,0,0),Vector3d(1,0,0),min,max,1,0,0);
	data[5] = PlanePart(position,Vector3d(-1,0,0),min,max,1,0,0);

	/*
	for(int i = 0;i < 6;i++)
	{
		cout << i << "\n";
		cout << "pos:\t" << data[i].position.print() << "\n";
		cout << "start:\t" << data[i].start.print() << "\n";
		cout << "end:\t" << data[i].end.print() << "\n";
		cout << "normal:\t" << data[i].normal.print() << "\n";	
	}
	*/

	//int i;
	//cin >> i;
}


Cube::~Cube(void)
{

}


TraceResult Cube::collision(const Ray &ray)
{
	TraceResult results[6];
	for(int i = 0;i < 6;i++)
	{
		results[i] = data[i].collision(ray);
	}

	//return results[0];

	TraceResult closest = TraceResult::empty;
	
	for(int i = 0;i < 6;i++)
	{
		if(results[i].hit && (results[i].distance < closest.distance))
		{
			closest = results[i];
		}
	}

	if(!closest.hit)
		return closest;

	closest.object = this;
	return closest;
}
